﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using HR.Appointments.Service.ValueObjects;
using HR.Appointments.Service.DisplayLists;

namespace HR.Appointments.Service.Test
{
    [TestFixture]
    public class TestActivityWithSlots
    {
        [SetUp]
        public void SetUp()
        {
            MockDao.SetUp();
        }

        [TearDown]
        public void TearDown()
        {
            MockDao.TearDown();
        }

        [Test]
        public void MockLoad()
        {
            var activity = DAO.Instance.Activity.GetInstance(1);
            Assert.IsNotNull(activity);

            Assert.AreEqual("Klipping", activity.Name);
            Assert.AreEqual(60, activity.Duration);
            Assert.AreEqual(1, activity.CompanyID);
        }

        //Getting the correct vacant slots taking into accoutn
        //the activity duration.
        // e.g.  a 30 minute windows should not be available for
        //       a 60 minute activity.
        [Test]
        public void TestActivitySlotsCalculation()
        {
            var list = new List<TimeList>();
            list.Add(new TimeList(TimeUnit.Create(7, 0), null, null));
            //here is an occupied slot from 7:30 to 8:00.
            list.Add(new TimeList(TimeUnit.Create(8, 0), null, null));
            list.Add(new TimeList(TimeUnit.Create(8, 30), null, null));
            //here is an occupied slot from 9:00 to 9:30.
            list.Add(new TimeList(TimeUnit.Create(9, 30), null, null));
            Assert.AreEqual(4, list.Count);

            var activity = DAO.Instance.Activity.GetInstance(1);
            Assert.IsNotNull(activity);

            //Get slot duration
            TimeDuration duration = TimeDuration.Create(30);
            Assert.IsNotNull(duration);

            int dur = activity.Duration / duration.DurationValue;

            //Create remove list of time that does't contain the nessicary duration
            List<TimeList> removeList = new List<TimeList>();
            //Loop through current list
            foreach (TimeList time in list)
            {
                //Create a compare list that contains items with nessicary duration
                List<TimeUnit> compareList = new List<TimeUnit>();
                var cmp = (TimeUnit)time.TimeUnit.Clone();
                for (int i = 0; i < dur; i++)
                {
                    compareList.Add((TimeUnit)cmp.Clone());
                    cmp = cmp.Add(duration.DurationValue);
                }

                //Compare it to current list and add all time items that doesn't cut it
                //to a separete removelist to be processed after the loop.
                foreach (TimeUnit tu in compareList)
                { 
                    if(!list.Contains(new TimeList(tu, null, null)))
                    {
                        removeList.Add(time);
                    }
                }
            }
            //remove all items that didn't cut it.
            foreach (TimeList removeItem in removeList)
            {
                list.Remove(removeItem);
            }

            Assert.AreEqual(TimeUnit.Create(8, 0).ToString(), list[0].Time);

        }
    }
}
